package com.woniu.Service.impl;

import com.woniu.Service.RoleMenuService;
import com.woniu.entity.RoleMenu;
import com.woniu.mapper.RoleMenuMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Gwj
 * @Date: 2024/7/4 15:14
 */
@Service
@Transactional
public class RoleMenuServiceImpl implements RoleMenuService {

    @Resource
    private RoleMenuMapper roleMenuMapper;

    /**
     * 角色的授权功能
     *
     * @param roleId
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    public int addRoleMenu(int roleId, List<RoleMenu> list) throws Exception {
        //先删除角色原有的权限
        Example example = new Example(RoleMenu.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("roleId", roleId);
        roleMenuMapper.deleteByExample(example);

        //再添加新的权限
        int i = roleMenuMapper.insertRoleMenu(list);
        return i;
    }
}
